package com.suntoon.framework.common.response;

import com.suntoon.framework.common.Constants;
import com.suntoon.framework.exception.MessageCenterException;

/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2021年06月21日 14时48分40秒
 * @Description:
 */
public class ResultBO<T> {
    private Integer code;
    private String msg;
    private T data;

    public ResultBO() {
    }

    public ResultBO(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultBO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回成功结果集
     *
     * @param data 数据
     * @return 结果集对象
     */
    public static ResultBO success(Object data) {
        return new ResultBO<>().setCode(Constants.SUCCESS_CODE).setMsg("").setData(data);
    }

    /**
     * 返回成功结果集
     *
     * @param msg 成功描述信息
     * @return 结果集对象
     */
    public static ResultBO success(String msg) {
        return new ResultBO<>().setCode(Constants.SUCCESS_CODE).setMsg(msg);
    }

    /**
     * @param msg  说明
     * @param data 数据
     * @return结 果集对象
     */
    public static ResultBO success(Object data, String msg) {
        return new ResultBO<>().setCode(Constants.SUCCESS_CODE).setData(data).setMsg(msg);
    }

    /**
     * 返回失败结果集
     *
     * @param msg 失败原因描述
     * @return 结果集对象
     */
    public static ResultBO fail(String msg) {
        return new ResultBO<>().setCode(Constants.FAIL_CODE).setMsg(msg);
    }

    /**
     * 返回失败结果集
     *
     * @param e 自定义异常类
     * @return 结果集对象
     */
    public static ResultBO fail(MessageCenterException e) {
        return new ResultBO<>().setCode(Constants.FAIL_CODE).setMsg(e.getMsg());
    }

    public Integer getCode() {
        return code;
    }

    public ResultBO<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public ResultBO<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return data;
    }

    public ResultBO<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "ResultBO{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
